<template>
  <view class="page-container">
    <!-- 顶部导航栏 -->
    <uni-nav-bar
      :border="false"
      title="退款备用金"
      fixed
      left-icon="back"
      @clickLeft="goBack"
    />

    <!-- 金额卡片 -->
    <view class="amount-card">
      <view class="amount-section">
        <text class="label">退款备用金</text>
        <text class="amount">¥ {{ reserveData.amount }}</text>
      </view>
      <view class="withdraw-section">
        <text class="withdraw-label">提现中</text>
        <text class="withdraw-amount">¥ {{ reserveData.withdrawing }}</text>
      </view>
      <view class="action-buttons">
        <view class="action-btn btn-withdraw" @click="handleWithdraw">
          <text>提现</text>
        </view>
        <view class="action-btn btn-recharge" @click="handleRecharge">
          <text>充值</text>
        </view>
      </view>
    </view>

    <!-- 明细列表 -->
    <view class="detail-section">
      <view class="detail-header">
        <text class="detail-title">明细</text>
        <view class="filter-btn" @click="showFilter">
          <text class="filter-text">筛选</text>
          <uni-icons type="down" size="14" color="#FFB200"></uni-icons>
        </view>
      </view>

      <!-- 列表内容 -->
      <view v-if="detailList.length > 0" class="detail-list">
        <view
          v-for="item in detailList"
          :key="item.id"
          class="detail-item"
          @click="handleDetailClick(item)"
        >
          <view class="detail-info">
            <text class="detail-type">{{ item.type }}</text>
            <text class="detail-time">{{ item.time }}</text>
          </view>
          <view class="detail-right">
            <text class="detail-amount" :class="{ positive: item.amount > 0 }">
              {{ item.amount > 0 ? "+" : "" }}¥ {{ Math.abs(item.amount) }}
            </text>
            <uni-icons type="right" size="16" color="#999"></uni-icons>
          </view>
        </view>
      </view>

      <!-- 空状态 -->
      <view v-else class="empty-state">
        <view class="empty-icon">
          <uni-icons type="list" size="80" color="#d9d9d9"></uni-icons>
        </view>
        <text class="empty-text">暂无数据~</text>
      </view>
    </view>

    <!-- 筛选弹窗 -->
    <RefundReserveFilter ref="filterRef" @confirm="handleFilterConfirm" />

    <!-- 充值弹窗 -->
    <RechargeModal ref="rechargeModalRef" @confirm="handleRechargeConfirm" />
  </view>
</template>

<script setup>
import { ref, onMounted } from "vue";
import RefundReserveFilter from "./components/RefundReserveFilter.vue";
import RechargeModal from "./components/RechargeModal.vue";

// 备用金数据
const reserveData = ref({
  amount: "0.00",
  withdrawing: "0.00",
});

// 明细列表
const detailList = ref([]);

// 筛选器引用
const filterRef = ref(null);

// 充值弹窗引用
const rechargeModalRef = ref(null);

// 返回上一页
const goBack = () => {
  uni.navigateBack();
};

// 处理提现
const handleWithdraw = () => {
  uni.navigateTo({
    url: "/pages/mine/balance/withdrawReserve",
  });
};

// 处理充值
const handleRecharge = () => {
  rechargeModalRef.value.open();
};

// 确认充值
const handleRechargeConfirm = (amount) => {
  console.log("充值金额:", amount);

  uni.showToast({
    title: `充值成功：¥${amount}`,
    icon: "none",
  });

  // TODO: 调用充值接口
  // TODO: 刷新备用金数据
};

// 显示筛选
const showFilter = () => {
  filterRef.value.open();
};

// 确认筛选
const handleFilterConfirm = (filterData) => {
  console.log("筛选条件:", filterData);

  const typeText = filterData.type === "income" ? "收入" : "支出";
  const tagCount = filterData.tags.length;

  uni.showToast({
    title: tagCount > 0 ? `已筛选 ${typeText} ${tagCount} 项` : "已清空筛选",
    icon: "none",
  });

  // TODO: 根据筛选条件加载数据
};

// 处理明细点击
const handleDetailClick = (item) => {
  console.log("点击明细:", item);
  // TODO: 跳转到明细详情
};

onMounted(() => {
  console.log("退款备用金页面加载完成");
  // TODO: 加载数据
});
</script>

<style lang="scss" scoped>
.page-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

// 金额卡片
.amount-card {
  background-color: #fff;
  margin: 20rpx 30rpx;
  padding: 40rpx 30rpx;
  border-radius: 16rpx;
}

.amount-section {
  display: flex;
  flex-direction: column;
  gap: 16rpx;
  margin-bottom: 24rpx;
}

.label {
  font-size: 28rpx;
  color: #333;
}

.amount {
  font-size: 80rpx;
  font-weight: 700;
  color: #333;
  line-height: 1.2;
}

.withdraw-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32rpx;
}

.withdraw-label {
  font-size: 26rpx;
  color: #666;
}

.withdraw-amount {
  font-size: 28rpx;
  color: #333;
  font-weight: 500;
}

.action-buttons {
  display: flex;
  gap: 20rpx;
}

.action-btn {
  flex: 1;
  padding: 24rpx 0;
  border-radius: 50rpx;
  text-align: center;
  font-size: 32rpx;
  font-weight: 600;
  b &:active {
    opacity: 0.8;
  }
}

.btn-withdraw {
  background-color: #f7efca;
  color: #ffbc00;
  border: 2rpx solid rgba(255, 255, 255, 0.5);
}

.btn-recharge {
  background-color: #ffb200;
  color: #fff;
}

// 明细部分
.detail-section {
  margin: 20rpx 30rpx;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.detail-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
}

.filter-btn {
  display: flex;
  align-items: center;
  gap: 4rpx;

  &:active {
    opacity: 0.7;
  }
}

.filter-text {
  font-size: 28rpx;
  color: #ffb200;
}

// 明细列表
.detail-list {
  background-color: #fff;
  border-radius: 12rpx;
  overflow: hidden;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24rpx 30rpx;
  border-bottom: 1rpx solid #f0f0f0;

  &:last-child {
    border-bottom: none;
  }

  &:active {
    background-color: #f9f9f9;
  }
}

.detail-info {
  display: flex;
  flex-direction: column;
  gap: 8rpx;
}

.detail-type {
  font-size: 28rpx;
  color: #333;
  font-weight: 500;
}

.detail-time {
  font-size: 24rpx;
  color: #999;
}

.detail-right {
  display: flex;
  align-items: center;
  gap: 12rpx;
}

.detail-amount {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;

  &.positive {
    color: #ff4d4f;
  }
}

// 空状态
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 120rpx 0;
  background-color: #fff;
  border-radius: 12rpx;
}

.empty-icon {
  margin-bottom: 20rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}
</style>
